#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2018-present Team CoreELEC (https://coreelec.org)

# copy amlogic autoscript files to part1 if needed
if [ "${SUBDEVICE}" != "Odroid_N2" ] && [ "${SUBDEVICE}" != "Odroid_C4" ] && [ "${SUBDEVICE}" != "LePotato" ] && [ "${SUBDEVICE}" != "LaFrite" ] ; then
  for f in $RELEASE_DIR/3rdparty/bootloader/*autoscript; do
    [ -e "$f" ] && mcopy "$f" ::
  done
fi

if [ -f $RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_cfgload ]; then
  mcopy -s $RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_cfgload "::/cfgload"
fi

# copy device trees to part1
for dtb in $RELEASE_DIR/3rdparty/bootloader/device_trees $RELEASE_DIR/3rdparty/bootloader/*.dtb $RELEASE_DIR/3rdparty/bootloader/dtb.img ; do
  [ -e "$dtb" ] && mcopy -s "$dtb" ::
done

if [ -f "$RELEASE_DIR/3rdparty/bootloader/config.ini" ]; then
  mcopy $RELEASE_DIR/3rdparty/bootloader/config.ini ::
fi

if [ "${SUBDEVICE}" != "Generic" ]; then
  if [ -f "$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_dtb.img" ]; then
    mcopy $RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_dtb.img "::/dtb.img"
  fi
  if [ -f "$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_u-boot" ] && [ "${SUBDEVICE}" != "LaFrite" ] ; then
    echo "Writing u-boot to $(basename $DISK)"
    dd if="$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_u-boot" of="$DISK" conv=fsync,notrunc bs=1 count=112 >"$SAVE_ERROR" 2>&1 || show_error
    dd if="$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_u-boot" of="$DISK" conv=fsync,notrunc bs=512 skip=1 seek=1 >"$SAVE_ERROR" 2>&1 || show_error
  fi

  if [ -f "$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_boot.ini" ]; then
    cp -p "$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_boot.ini" "$LE_TMP/boot.ini"
    sed -e "s/@BOOT_UUID@/$UUID_SYSTEM/" \
        -e "s/@DISK_UUID@/$UUID_STORAGE/" \
        -i "$LE_TMP/boot.ini"
    mcopy "$LE_TMP/boot.ini" ::
  fi

  if [ "${SUBDEVICE}" == "Odroid_N2" -o "${SUBDEVICE}" == "Odroid_C4" ]; then
    if [ -f "$RELEASE_DIR/3rdparty/bootloader/hk-boot-logo-1080.bmp.gz" ]; then
      mcopy $RELEASE_DIR/3rdparty/bootloader/hk-boot-logo-1080.bmp.gz "::/boot-logo-1080.bmp.gz"
    fi
    if [ -f "$RELEASE_DIR/3rdparty/bootloader/timeout-logo-1080.bmp.gz" ]; then
      mcopy $RELEASE_DIR/3rdparty/bootloader/timeout-logo-1080.bmp.gz ::
    fi
  fi

  if [ "${SUBDEVICE}" == "LePotato" -o "${SUBDEVICE}" == "LaFrite" ]; then
    if [ -f "$RELEASE_DIR/3rdparty/bootloader/boot-logo-1080.bmp.gz" ]; then
      mcopy $RELEASE_DIR/3rdparty/bootloader/boot-logo-1080.bmp.gz ::
    fi
    if [ -f "$RELEASE_DIR/3rdparty/bootloader/timeout-logo-1080.bmp.gz" ]; then
      mcopy $RELEASE_DIR/3rdparty/bootloader/timeout-logo-1080.bmp.gz ::
    fi
    if [ -f "$RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_chain_u-boot" ]; then
      mcopy $RELEASE_DIR/3rdparty/bootloader/${SUBDEVICE}_chain_u-boot "::/u-boot.bin"
    fi
    if [ -f "$RELEASE_DIR/3rdparty/bootloader/libretech_chain_boot" ]; then
      mcopy $RELEASE_DIR/3rdparty/bootloader/libretech_chain_boot "::/boot.scr"
    fi
  fi

fi
